package com.uenpay.im.configuration;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;

import com.qiyunxin.android.http.common.SystemUtil;
import com.qiyunxin.android.http.config.AppConfig;
import com.uenpay.im.IMApplication;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 缓存数据
 *
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class QYXIMConfiguration {
    public final String root_path;
    public static ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    private SharedPreferences sharePreferences;

    public String getSharePicPath() {
        return Environment.getExternalStorageDirectory() + "/share.jpg";
    }

    public String getRecorderPath() {
        return root_path + "record.amr";
    }

    public QYXIMConfiguration(String custid) {
        String applicationName = IMApplication.getAppContext().getPackageName();
        sharePreferences = IMApplication.getAppContext().getSharedPreferences("config_" + custid, 0);
        root_path = SystemUtil.GetExternalStorageDir() + applicationName + File.separator;
    }

    public void setMsgShake(int shake) {
        Editor editor = sharePreferences.edit();
        editor.putInt("shake", shake);
        editor.commit();
    }

    public void setMsgVoice(int voice) {
        Editor editor = sharePreferences.edit();
        editor.putInt("voice", voice);
        editor.commit();
    }

    /**
     * 是否通知
     *
     * @param is_notice
     */
    public void setMsgNotic(int is_notice) {
        Editor editor = sharePreferences.edit();
        editor.putInt("is_notice", is_notice);
        editor.commit();
    }

    public int getMsgNotice() {
        return sharePreferences.getInt("is_notice", 1);
    }

    public int getMsgShake() {
        return sharePreferences.getInt("shake", 0);
    }

    public int getMsgVoice() {
        return sharePreferences.getInt("voice", 1);
    }

    public long getFriendLastUpdateId() {
        return sharePreferences.getLong("friendLastUpdateId", 0);
    }

    public void setFriendLastUpdateId(long last_update_id) {
        Editor editor = sharePreferences.edit();
        editor.putLong("friendLastUpdateId", last_update_id);
        editor.commit();
    }

    public String getUserName() {
        return sharePreferences.getString("userName", "");
    }

    public void setUserName(String userName) {
        Editor editor = sharePreferences.edit();
        editor.putString("userName", userName);
        editor.commit();
    }

    public String getUserSignature() {
        return sharePreferences.getString("UserSignature", "");
    }

    public void setUserSignature(String UserSignature) {
        Editor editor = sharePreferences.edit();
        editor.putString("UserSignature", UserSignature);
        editor.commit();
    }

    public void setIsFirstLogin(boolean isfirstlogin) {
        Editor editor = sharePreferences.edit();
        editor.putBoolean("is_first_login", isfirstlogin);
        editor.commit();
    }

    public boolean getIsFirstLogin() {
        return sharePreferences.getBoolean("is_first_login", false);
    }

    public int getUnreadChatMsgCount() {
        return sharePreferences.getInt("unreadChatMsgCount", 0);
    }

    public void setUnreadChatMsgCount(int val) {
        Editor editor = sharePreferences.edit();
        editor.putInt("unreadChatMsgCount", val);
        editor.commit();
    }

    public void setIsNewFriend(int val) {
        Editor editor = sharePreferences.edit();
        editor.putInt("new_friend", val);
        editor.commit();
    }

    public int getIsNewFriend() {
        return sharePreferences.getInt("new_friend", 0);
    }

    public int getDynamicUnreadMsgCount() {
        return sharePreferences.getInt("dynamic_unread_count", 0);
    }

    public void setDynamicUnreadMsgCount(int dynamic_unread_count) {
        Editor editor = sharePreferences.edit();
        editor.putInt("dynamic_unread_count", dynamic_unread_count);
        editor.commit();
    }

    public String getDynamicUnreadMsgCustId() {
        return sharePreferences.getString("dynamic_unread_custid", "");
    }

    public void setDynamicUnreadMsgCustId(String dynamic_unread_custid) {
        Editor editor = sharePreferences.edit();
        editor.putString("dynamic_unread_custid", dynamic_unread_custid);
        editor.commit();
    }

    public String getDynamicLastSendCustId() {
        return sharePreferences.getString("last_send_dynamic_cust_id", "");
    }

    public void setDynamicLastSendCustId(String last_send_dynamic_cust_id) {
        Editor editor = sharePreferences.edit();
        editor.putString("last_send_dynamic_cust_id", last_send_dynamic_cust_id);
        editor.commit();
    }

    public void setDynamicBg(String dynamicBg) {
        Editor editor = sharePreferences.edit();
        editor.putString("dynamicBg", dynamicBg);
        editor.commit();
    }

    public String getDynamicBg() {
        return sharePreferences.getString("dynamicBg", "");
    }

    /**
     * 设置联系人Json
     *
     * @param contacts_json
     */
    public void setContactsJson(String contacts_json) {
        Editor editor = sharePreferences.edit();
        editor.putString("contacts_json", contacts_json);
        editor.commit();
    }

    /***
     * 获取联系人json
     */
    public String getContactsJson() {
        return sharePreferences.getString("contacts_json", "");
    }

    /**
     * 设置选中的公司id
     *
     * @param company_id
     */
    public void setSelectedCompanyId(int company_id) {
        Editor editor = sharePreferences.edit();
        editor.putInt("company_id", company_id);
        editor.commit();
    }

    /**
     * 设置选中的公司名称
     *
     * @param company_name
     */
    public void setSelectedCompany(String company_name) {
        Editor editor = sharePreferences.edit();
        editor.putString("company_name", company_name);
        editor.commit();
    }

    /***
     * 获取选中的公司名称
     */
    public String getSelectedCompany() {
        return sharePreferences.getString("company_name", "");
    }

    /***
     * 获取选中的公司id
     */
    public int getSelectedCompanyId() {
        return sharePreferences.getInt("company_id", 0);
    }

    /**
     * 设置陌生人备注Json数据
     *
     * @param cust_id
     * @param friends_remark_json 备注json
     */
    public void setStrangerFriendsJson(String cust_id, String friends_remark_json) {
        Editor editor = sharePreferences.edit();
        editor.putString("friends_remark_json_" + cust_id, friends_remark_json);
        editor.commit();
    }

    /***
     * 获取陌生人备注json
     */
    public String getStrangerFriendsJson(String cust_id) {
        return sharePreferences.getString("friends_remark_json_" + cust_id, "");
    }

    /**
     * 设置好友备注名称
     *
     * @param cust_id
     * @param remarkName
     */
    public void setFriendsRemarkName(String cust_id, String remarkName) {
        Editor editor = sharePreferences.edit();
        editor.putString("remark_name" + cust_id, remarkName);
        editor.commit();
    }

    /**
     * 获取好友备注名称
     *
     * @param cust_id
     * @return
     */
    public String getFriendsRemarkName(String cust_id) {
        return sharePreferences.getString("remark_name" + cust_id, "");
    }

    /**
     * 获取软键盘高度
     *
     * @return
     */
    public int getKeyBoardHeight() {
        return sharePreferences.getInt("key_board_height", 0);
    }

    /**
     * 设置软键盘高度
     *
     * @param key_board_height
     */
    public void setKeyBoardHeight(int key_board_height) {
        Editor editor = sharePreferences.edit();
        editor.putInt("key_board_height", key_board_height);
        editor.commit();
    }

    /**
     * 获取用户添加的表情
     *
     * @param cust_id
     * @return
     */
    public String getCustomFaceJson(String cust_id) {
        return sharePreferences.getString("custom_face_json_" + cust_id, "");
    }

    /**
     * 设置用户添加的表情
     *
     * @param cust_id
     * @return
     */
    public void setCustomFaceJson(String cust_id, String custom_face_json) {
        Editor editor = sharePreferences.edit();
        editor.putString("custom_face_json_" + cust_id, custom_face_json);
        editor.commit();
    }

    /**
     * 设置视频通话地址
     *
     * @param roomserver
     */
    public void setRoomServer(String roomserver) {
        Editor editor = sharePreferences.edit();
        editor.putString("roomserver", roomserver);
        editor.commit();
    }

    /**
     * @param base_api_url
     */
    public void setBaseApiUrl(String base_api_url) {
        Editor editor = sharePreferences.edit();
        editor.putString("base_api_url", base_api_url);
        editor.commit();
    }

    public String getBaseApiUrl() {
        String url = com.uenpay.util.common.AppConfig.IS_RELEASE ? "http://im.uenpay.com/" : "http://wfb.uenpay.com/";
        return sharePreferences.getString("base_api_url", url);
    }

}
